// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import com.incrediblesoft.redjey.event.FocusEvent;
import com.incrediblesoft.redjey.event.KeyEvent;

import java.util.Hashtable;

public class Frame extends Window
{
    protected TextComponent header;
    protected ScrollPane contentScrollPane;
    protected MenuBar menuBar;

    protected Component containerFocusOwner;

    public Frame(String title)
    {
        this(title, null);
    }

    public Frame(String title, String styleClassName)
    {
        super(styleClassName);

        header = getDefaultHeader();
        if (header != null)
            header.parent = this;

        add(header);

        setLayout(null);
        setTitle(title);
        setState(NORMAL);
    }

    public String getName()
    {
        return "Frame";
    }

    protected Dimension calcPreferredSize(int paddingHorizontal, int paddingVertical, CustomFont font, Hashtable attributes)
    {
        return new Dimension( UIManager.getCanvas().getWidth(),
                              UIManager.getCanvas().getHeight() );
    }

    /**
     * Sets the title of the frame
     * @param title
     */
    public void setTitle(String title)
    {
        if (header != null)
        {
            header.setText(title);
        }
    }

    public Container getContentPane()
    {
        if (contentScrollPane == null)
            setContentPane(new Container());
        
        return (Container) contentScrollPane.getView();
    }

    public void setContentPane(Container contentPane)
    {
        this.remove(contentScrollPane);
        contentScrollPane = new ScrollPane(contentPane, (String) currentStyle.attributes.get("content-scrollpane-class"));
        contentScrollPane.getVerticalBar().focusContext = this.focusContext + 1;
        contentScrollPane.getHorizontalBar().focusContext = this.focusContext + 1;
        contentPane.setAlignment(contentPane.getAlignment() | (H_EXPAND | V_EXPAND));
        add(contentScrollPane);
        invalidate();
    }

    public void setMenuBar(MenuBar menuBar)
    {
        if (this.menuBar != null)
            remove(this.menuBar);

        this.menuBar = menuBar;

        if (menuBar != null)
        {
            insertAt(menuBar, 0);
            menuBar.focusContext = 1;
        }
        invalidate();
    }

    public MenuBar getMenuBar()
    {
        return menuBar;
    }

    protected TextComponent getDefaultHeader()
    {
        Label headerLabel = new Label(null);
        headerLabel.setContentAlignment(HCENTER_ALIGNMENT | TOP_ALIGNMENT);
        return headerLabel;
    }

    public Component getHeader()
    {
        return header;
    }

    /*protected void processFocusEvent(FocusEvent event)
    {
        super.processFocusEvent(event);
        switch(event.getID())
        {
            case FocusEvent.FOCUS_GAINED:
                UIManager.addKeyListener(this);
                break;

            case FocusEvent.FOCUS_LOST:
                UIManager.removeKeyListener(this);
                break;
        }
    }*/

    public void setVisible(boolean visible)
    {
        if (visible)
            UIManager.addKeyListener(this);
        else
            UIManager.removeKeyListener(this);

        super.setVisible(visible);
    }

    protected void processKeyEvent(KeyEvent keyEvent)
    {
        switch(keyEvent.getID())
        {
            case KeyEvent.KEY_PRESSED:
                if (menuBar != null)
                {
                    if (keyEvent.getKeyCode() == UIManager.KEY_SOFT_LEFT)
                    {
                        if (!menuBar.focusCaptured)
                            menuBar.captureFocus();
                        else
                            menuBar.looseFocus();
                    }
                }
                break;
        }

        super.processKeyEvent(keyEvent);
    }


    public void setState(int index)
    {
        super.setState(index);
        
        if (header != null)
        {
            String headerStyle = (String) currentStyle.attributes.get("header-class");
            if (headerStyle != null) header.setStyleClass(headerStyle);
        }
        if (contentScrollPane != null)
        {
            String style = (String) currentStyle.attributes.get("content-scrollpane-class");
            if (style != null) contentScrollPane.setStyleClass(style);
        }
        if (menuBar != null)
        {
            String softkeyBarStyle = (String) currentStyle.attributes.get("menubar-class");
            if (softkeyBarStyle != null) menuBar.setStyleClass(softkeyBarStyle);
        }
    }

    protected void validateComponent()
    {
        int totalHeight = clientSize.height;

        if (header != null)
        {
            header.setSize(new Dimension(getWidth() - currentStyle.getMinWidth(), Math.min(totalHeight, header.getPreferredSize().height)));
            header.setLocation(insets[0], insets[2]);
            totalHeight -= header.getHeight();
        }

        if (totalHeight > 0 && menuBar != null)
        {
            menuBar.setSize(getWidth() - currentStyle.getMinWidth(), Math.min(totalHeight, menuBar.getPreferredSize().height));
            menuBar.setLocation(insets[0], insets[2] + getHeight() - currentStyle.getMinHeight() - menuBar.getHeight());
            totalHeight -= menuBar.getHeight();
        }

        if (totalHeight > 0 && contentScrollPane != null)
        {
            contentScrollPane.setSize(getWidth() - currentStyle.getMinWidth(), totalHeight);
            contentScrollPane.setLocation(insets[0], insets[2] + (header != null ? header.getHeight() : 0));
            //contentPane.setFocused(true);
        }
    }

}
